<template>
  
  <div class="main-container">
    <el-button @click="handleBatchDelete">批量删除</el-button>
      <el-table
          :data="activityData"
          border
          :header-cell-style="headerCellStyle"
          @selection-change="handleSelectionChange"
      >
        <!-- 添加多选框列 -->
        <el-table-column type="selection" width="55"></el-table-column>
        <!-- 定义列 -->
        <el-table-column
          v-for="item in activityDataLabel"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
          show-overflow-tooltip
          :width="item.width ? item.width : 80"
        >
        <template #default="{ row }" v-if="item.prop === 'avatar'">
              <div style="display: flex; justify-content: center; align-items: center;">
                <img :src="row.avatar" alt="发布者头像" style="width: 50px; height: 30px; object-fit: cover;">
              </div>
            </template>
          <template #default="{ row }" v-else-if="item.prop === 'imageUrl'">
              <div style="display: flex; justify-content: center; align-items: center;">
                <img :src="row.imageUrl" alt="活动图" style="width: 80px; height: 30px; object-fit: cover;">
              </div>
            </template>
            <template #default="{ row }" v-else>
              <div style="text-align: center;">
                {{ row[item.prop] }}
              </div>
            </template>
        </el-table-column>
        <el-table-column label="操作" :width="200">
            <template #default="{ row }">
              <div class="action-column">
                <el-button @click="handleDelete(row)">删除</el-button>
                <el-button @click="handleExamine(row)">审核通过</el-button>
              </div>
            </template>
          </el-table-column>
          
      </el-table>
    <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          v-model:current-page="pagination.pageNo"
          :current-page="pagination.pageNo"
          :page-size="pagination.pageSize"
          layout="total,prev, pager, next"
          :total="pagination.total"
        >
        </el-pagination>
      </div>  
  </div>
  </template>
  
<script setup>
import { ref,onMounted } from 'vue';
import { getActivityListAPI,updateActivityStatusAPI } from '../../../../api/admin/check'
import { delActivityAPI,delActivitySAPI } from '../../../../api/admin.js'
import { ElMessage } from 'element-plus';
import dayjs from 'dayjs';
const headerCellStyle = () => {
    return {
      'text-align': 'center',
      color: 'black',
      'background-color': '#f5f7f9'
    };
  };
const activityDataLabel = ref([
    {
      prop: 'authorId',
      label: '发布者id',
      width: 100,
      sortable: true
    },
    {
      prop: 'activityId',
      label: '活动id',
      width: 100,
      sortable: true
    },
    {
      prop: 'title',
      label: '活动名',
      width: 150,
    },
    {
      prop: 'content',
      label: '活动介绍',
      width: 150,
      sortable: true
    },
    {
      prop: 'avatar',
      label: '发布者头像',
      width: 150,
    },
    {
      prop: 'imageUrl',
      label: '活动图',
      width: 150,
    },
    {
      prop: 'name',
      label: '发布人',
      width: 100,
    },
    {
      prop: 'isShow',
      label: '状态',
    },
    {
      prop:'activityNum',
      label:"需要人数",
      width: 100,
    },
    {
      prop: 'activityPlace',
      label: '活动地点',
      width: 150,
    },
    {
      prop: 'startTime',
      label: '开始时间',
      width: 150,
      sortable: true,
    },
    {
      prop: 'endTime',
      label: '结束时间',
      width: 150,
      sortable: true,
    },
]); 
const activityData = ref([]);
const pagination = ref({
  pageNo: 1,
  pageSize: 10,
  total: 0,
});
const selectedRows = ref([]);
const handleSelectionChange = (rows) => {
    selectedRows.value = rows;
}
const handleBatchDelete = async () => {
    if (selectedRows.value.length === 0) {
      console.log('请选择要删除的待审核活动');
      return;
    }
    const ids = selectedRows.value.map(row => row.activityId.toString()).join(',');
    try {
      const response = await delActivitySAPI({ids});
      if (response.data.code === 0) {
        // activityData.value = activityData.value.filter(item =>!ids.includes(item.activityId));
        await fetchExamineActivityList();
        console.log('批量删除成功');
      } else {
        console.error('批量删除失败:', response.data.message);
      }
    } catch (error) {
      console.error('批量删除失败:', error);
    }
  };
const handleDelete = async (row) => {
  const response = await delActivityAPI({activityId:row.activityId});
  // console.log(response.data);
  await fetchExamineActivityList();
};
const handleExamine = async (row) => {
  try {
        let params={
          activityId:row.activityId,
          status:0
        }
        const response = await updateActivityStatusAPI(params)
        // console.log(response.data);
        if (response.data.code === 0) {
            // 从表格数据中移除该活动记录
            activityData.value = activityData.value.filter(item => item.activityId!== row.activityId);
            // 更新总数据量
            pagination.value.total--;
            ElMessage.success('审核通过，该活动已从列表移除');
        } else {
            ElMessage.error('审核失败: ' + response.data.message);
        }
    } catch (error) {
        ElMessage.error('操作发生错误: ' + error.message);
    }
};
const handleSizeChange = async (newSize) => {
    pagination.value.pageSize = newSize;
    pagination.value.pageNo = 1;
    await fetchExamineActivityList();
 };
const handleCurrentChange = async (newPage) => {
    pagination.value.pageNo = newPage;
    await fetchExamineActivityList();
  };

const fetchExamineActivityList = async () => {
  try {
    let param = {
      pageNo: pagination.value.pageNo,
      pageSize: pagination.value.pageSize
    };
    const response = await getActivityListAPI(param);
    // console.log(response.data);
    const formattedData = response.data.data.list.map(item => {
        return {
          ...item,
          startTime: dayjs(item.startTime).format('YYYY-MM-DD HH:mm'),
          endTime: dayjs(item.endTime).format('YYYY-MM-DD HH:mm')
        };
      });
      // 将后端返回的活动列表数据赋值给 activityData
      activityData.value = formattedData;
    pagination.value.total = response.data.data.total;
   } catch (error) {
    console.error('获取活动列表失败:', error);
  }
};
onMounted(async () => {
    await fetchExamineActivityList();
});
</script>
<style lang="less" scoped>
.main-container{
  height: calc(100% - 64px);
  // height: 100%;
  // background-color: aqua;
}
.el-table {
    // overflow-y: scroll;
    height: 100%;
    // background-color: red;
    .action-column {
      height: 100%;
      display: flex;
      flex-wrap: nowrap;
      /* 确保按钮不换行 */
      white-space: nowrap;
      /* 防止按钮内的文本换行 */
    }
}
.pagination-container {
  display: flex;
  // background-color: rgb(55, 224, 21);
  justify-content: center;
  // margin-top: 20px;
}
</style>